package com.rzl.expense.web.controller.system;

import javax.servlet.http.HttpServletResponse;

import com.rzl.expense.common.core.domain.entity.SysRole;
import com.rzl.expense.common.utils.poi.ExcelUtil;
import com.rzl.expense.agent.DataAgentService;
import com.rzl.expense.system.domain.GroupUserRole;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rzl.expense.common.annotation.Log;
import com.rzl.expense.common.core.controller.BaseController;
import com.rzl.expense.common.core.domain.AjaxResult;
import com.rzl.expense.common.enums.BusinessType;
import com.rzl.expense.system.service.IGroupUserRoleService;
import com.rzl.expense.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 集团角色关联Controller
 *
 * @author expense
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/system/group/role")
public class GroupUserRoleController extends BaseController {
    @Autowired
    private IGroupUserRoleService groupUserRoleService;

    @Autowired
    private DataAgentService dataAgentService;

    /**
     * 查询集团角色关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:group:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(GroupUserRole groupUserRole) {
        startPage();
        List<GroupUserRole> list = groupUserRoleService.selectGroupUserRoleList(groupUserRole);
        return getDataTable(list);
    }

    /**
     * 导出集团角色关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:group:role:export')")
    @Log(title = "集团角色关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GroupUserRole groupUserRole) {
        List<GroupUserRole> list = groupUserRoleService.selectGroupUserRoleList(groupUserRole);
        ExcelUtil<GroupUserRole> util = new ExcelUtil<GroupUserRole>(GroupUserRole.class);
        util.exportExcel(response, list, "集团角色关联数据");
    }


    @PostMapping("/fresh")
    public AjaxResult freshRole() {

        List<SysRole> roles = (List<SysRole>) dataAgentService.getData("select * from sys_role where prv_id=220000", SysRole.class);
        if (roles.size() > 0) {
            for (SysRole role : roles) {
                GroupUserRole groupUserRole = groupUserRoleService.selectGroupUserRoleByRoleId(role.getRoleId());
                if (groupUserRole != null) {
                    groupUserRole.setRoleName(role.getRoleName());
                    groupUserRoleService.updateGroupUserRole(groupUserRole);
                } else {
                    groupUserRole = new GroupUserRole();
                    groupUserRole.setRoleId(role.getRoleId());
                    groupUserRole.setRoleName(role.getRoleName());
                    groupUserRole.setAuthId(3l);
                    groupUserRoleService.insertGroupUserRole(groupUserRole);
                }

            }
        }
        return AjaxResult.success("集团角色同步成功");
    }

    /**
     * 获取集团角色关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:group:role:query')")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable("roleId") String roleId) {
        return success(groupUserRoleService.selectGroupUserRoleByRoleId(roleId));
    }

    /**
     * 新增集团角色关联
     */
    @PreAuthorize("@ss.hasPermi('system:group:role:add')")
    @Log(title = "集团角色关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GroupUserRole groupUserRole) {
        return toAjax(groupUserRoleService.insertGroupUserRole(groupUserRole));
    }

    /**
     * 修改集团角色关联
     */
    @PreAuthorize("@ss.hasPermi('system:group:role:edit')")
    @Log(title = "集团角色关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GroupUserRole groupUserRole) {
        return toAjax(groupUserRoleService.updateGroupUserRole(groupUserRole));
    }

    /**
     * 删除集团角色关联
     */
    @PreAuthorize("@ss.hasPermi('system:group:role:remove')")
    @Log(title = "集团角色关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable String[] roleIds) {
        return toAjax(groupUserRoleService.deleteGroupUserRoleByRoleIds(roleIds));
    }
}
